<?php
namespace app\admin\controller;

use think\Request;
use app\admin\model\SiteConfig;

class Site extends Base
{
    protected $site_model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->site_model = new SiteConfig();
    }

    /**
     * 网站基本设置
     */
    public function index()
    {
        $data = $this->site_model->where('status', 1)->where('type', 1)->select();

        // 网站logo
        $logo = $this->site_model->where('field', 'logo')->find();

        $this->assign('data', $data);
        $this->assign('logo', $logo);
        return $this->fetch();
    }

    /**
     * 网站联系方式设置
     */
    public function link()
    {
        $data = $this->site_model->where('status', 1)->where('type', 2)->select();

        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 修改设置
     */
    public function modify($id, $field, $value)
    {
        if (Request::instance()->isAjax()) {
            $result = $this->site_model->save(['value'=>$value], ['id'=>$id]);

            if ($result !== false) {
                return ['code'=>2, 'msg'=>config('msg.1003')];
            } else {
                return ['code'=>0, 'msg'=>config('msg.1004')];
            }
        }
    }

    /**
     * 脚本设置
     */
    public function script()
    {
        $script_model = new \app\admin\model\ScriptConfig();

        $data = $script_model->find(1);

        if (Request::instance()->isPOST()) {
            $head = (input('post.head')!==null) ? input('post.head') : '';

            $footer = (input('post.footer')!==null) ? input('post.footer') : '';

            try {
                $data->save([
                    'head' => $head,
                    'footer' => $footer,
                ]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误代码:" . $msg);
            }
            $this->success(config('msg.1003'), 'index');
        }

        $this->assign('data', $data);
        return $this->fetch();
    }
}
